package com.zj.core.tools

import com.fileutils.BaseConstant
import com.fileutils.BaseConstant.PATTERN_SIGN
import com.fileutils.BaseConstant.PATTERN_SYMBOL
import com.fileutils.NumberUtils
import com.jniutils.DataCollUtil
import com.systemutils.SystemUtil
import java.util.regex.Pattern

class VoiceFormatTools {
    companion object {
        /**
         * 对录入数据进行格式化处理
         */
        fun formatVoiceValue(voiceKey: String, voiceStr: String): String {
            if (voiceStr == "") {
                return voiceStr
            }

            when (voiceKey) {
                "宫底" -> {
                    return voiceStr //arab2Char(voiceStr)
                }

                "大便" -> {
                    return excrementFormat(voiceStr)
                }

                "小便" -> {
                    return urinateFormat(voiceStr)
                }

                "血压" -> {
                    return replaceChar(
                        BaseConstant.PATTERN_BLOOD,
                        voiceStr,
                        0
                    )
                }

                "胎心" -> {
                    return replaceChar(PATTERN_SIGN, voiceStr, 0)
                }

                "体温" -> {
                    return when {
                        voiceStr.contains("摄氏度") -> voiceStr.replace("摄氏度","")
                        voiceStr.contains("摄氏") -> voiceStr.replace("摄氏","")
                        voiceStr.substring(voiceStr.length - 1, voiceStr.length) != "度" -> replaceChar(PATTERN_SIGN, voiceStr, 1)
                        else -> voiceStr
                    }
                }

                "脉搏", "呼吸", "血氧", "吸氧", "胎动", "心率" -> {
                    return voiceStr.replace("[到]".toRegex(), "-")
                }

                "血糖" -> {
                    return replaceChar(PATTERN_SIGN, voiceStr, 1)
                }
            }
            return voiceStr
        }

        /**
         * 药品名称
         * 优先处理
         */
        private var mTemplateBuild: StringBuilder = StringBuilder()

        fun drugFirstSolve(obj: String, mDrugsListStr: String): String {
            var drugsIndex = 0
            mTemplateBuild.clear()

            val mArr = DataCollUtil.getProjectList(BaseConstant.PATTERN_DRUG, obj, mDrugsListStr)
            for (objBean in mArr) {
                val value = objBean.value               //正则匹配内容
                val startIndex = obj.indexOf(value)

                mTemplateBuild.append(
                    VoiceFormatTools.formatUnit(
                        NumberUtils.CharDateFormat(
                            obj.substring(
                                drugsIndex,
                                startIndex
                            )
                        )
                    ) + value
                )
                drugsIndex = startIndex + value.length
            }
            mTemplateBuild.append(
                VoiceFormatTools.formatUnit(
                    NumberUtils.CharDateFormat(
                        obj.substring(
                            drugsIndex,
                            obj.length
                        )
                    )
                )
            )
            return VoiceFormatTools.medicineTypeTransform(mTemplateBuild.toString())
        }

        /**
         * 替换
         */
        private fun replaceChar(pattern: Int, voicestr: String, status: Int): String {
            val value = DataCollUtil.patternValue(pattern, voicestr)
            return if (value != "") {
                value
            } else {
                return if (status == 0) {
                    voicestr.replace(
                        "[.|点|减|碘|:|：|年|斜杠|钢|肛|-]".toRegex(),
                        "/"
                    ).replace("[到|道|导|盗]".toRegex(), "-")
                } else {
                    voicestr.replace(
                        "[.|点|减|度|碘|斜杠|钢|肛]".toRegex(),
                        "."
                    ).replace("[到|道|导|盗]".toRegex(), "-")
                }
            }
        }

        /**
         * 计量单位格式化
         */
        fun formatUnit(source: String): String {
            return source.replace("厘米", "cm")
                .replace("毫米", "mm")
                .replace("毫升", "ml")
                .replace("毫克", "mg")
                .replace("微克", "μg")
                .replace("升", "L")
                .replace("克", "g")
                .replace("米", "m")
        }

        /**
         * 提交内容去除括号
         *
         * @param commitValue
         * @return
         */
        fun wipeBracketOnCommitValue(commitValue: String): String {
            val sb = StringBuilder(commitValue)

            val indexList = SystemUtil.getGBKStrIndex(BaseConstant.PATTERN_NOBLACK, sb.toString())
            for (objBean in indexList) {
                val start = objBean.startIndex
                val end = objBean.endIndex
                val capValue = objBean.value
                val resultStr = DataCollUtil.patternValue(PATTERN_SYMBOL, capValue)
                if (resultStr == null || resultStr.isEmpty()) {          //非阴性阳性
                    sb.replace(start + 1, start + 2, " ").replace(end, end + 1, " ")
                }
            }
            return sb.toString()
        }

        /**
         * 阴性阳性转换
         *
         * @param transform
         */
        fun medicineTypeTransform(transform: String): String {
            return when (transform) {
                "阴性" -> "-"
                "阳性" -> "+"
                "(-)" -> "-"
                "(+)" -> "+"
                else -> transform
            }
        }

        /**
         * 小便转换
         */
        private fun urinateFormat(urinate: String): String {
            return if (urinate == "S" || urinate == "s" || urinate == "失禁") {
                "※"
            } else if (urinate == "导尿") {
                "C"
            } else if (urinate.contains("导尿")) {
                val unitCount = urinate.indexOf("导尿") + 2
                if (unitCount < urinate.length) {
                    urinateCountFormat(
                        urinate.substring(
                            unitCount,
                            unitCount + 1
                        )
                    ) + "/C"
                } else {
                    urinate
                }
            } else {
                urinate
            }
        }

        /**
         * 小便次数转换
         */
        private fun urinateCountFormat(urinateCount: String): String {
            when (urinateCount) {
                "伊" -> return "1"
                "阴" -> return "1"
                "啊" -> return "2"
                "酸" -> return "3"
                "术" -> return "4"
                "塞" -> return "4"
                "无" -> return "5"
                "留" -> return "6"
                "求" -> return "9"
                "吧" -> return "8"
                "时" -> return "10"
            }
            return urinateCount
        }

        /**
         * 大便转换
         */
        private fun excrementFormat(excrementStr: String): String {
            when (excrementStr) {
                "0", "零" -> {
                    return "0"
                }

                "S", "s", "失禁" -> {
                    return "※"
                }

                else -> {
                    if (excrementStr.length == 5) {                         //例：一灌一后二
                        val mStart = excrementStr.substring(0, 1)
                        val mCenter = excrementStr.substring(2, 3)
                        val mEnd =
                            excrementStr.substring(excrementStr.length - 1, excrementStr.length)
                        if (checkNumber(mStart) && checkNumber(
                                mCenter
                            ) && checkNumber(mEnd)
                        ) {
                            return if (mCenter == "1") {
                                "$mStart $mEnd/E"
                            } else {
                                "$mStart $mEnd/${mCenter}E"
                            }
                        }
                    } else if (excrementStr.length == 4) {                   //例：灌一后三
                        val mStart = excrementStr.substring(1, 2)
                        val mEnd =
                            excrementStr.substring(excrementStr.length - 1, excrementStr.length)
                        if (checkNumber(mStart) && checkNumber(
                                mEnd
                            )
                        ) {
                            return if (mStart == "1") {
                                "$mEnd/E"
                            } else {
                                "$mEnd/${mStart}E"
                            }
                        }
                    }
                }
            }
            return excrementStr
        }

        /**
         * 判断是否为阿拉伯数字
         */
        private fun checkNumber(value: String): Boolean {
            val pattern = Pattern.compile("^[\\d]*\$")
            return pattern.matcher(value).matches()
        }

        /**
         * 正则关键字重新组合，同类型词汇，长词在前，短词在后
         */
        fun afreshPatternSplit(patternStr: Any, splitSymbol: String = "|"): Any {
            val patternSort: ArrayList<String>? = ArrayList()
            val sortArray: ArrayList<String>? = ArrayList()
            var statue = 0

            if (patternStr is String) {
                val patternList = patternStr.split(splitSymbol).sortedBy { -it.length }         //排序
                for (list in patternList) {
                    patternSort?.add(list)
                }
                statue = 1
            }

            if (patternStr is ArrayList<*>) {
                val patternList = (patternStr as ArrayList<String>).sortedBy { -it.length }     //排序
                for (list in patternList) {
                    patternSort?.add(list)
                }
                statue = 2
            }

            if (patternSort != null && patternSort.size != 0) {
                //去重
                patternSort.forEachIndexed { _, s ->
                    if (sortArray?.indexOf(s) == -1) {
                        sortArray.add(s)
                    }
                }

                if (statue == 2) {
                    return sortArray!!
                }

                var patternSortValue = ""
                for (itemStr in sortArray!!) {
                    patternSortValue = "$patternSortValue$itemStr$splitSymbol"
                }
                if (patternSortValue.substring(
                        patternSortValue.length - 1,
                        patternSortValue.length
                    ) == "|"
                ) {
                    patternSortValue = patternSortValue.substring(0, patternSortValue.length - 1)
                }
                return patternSortValue
            }
            return patternStr
        }
    }
}